import paramiko
import paramiko,datetime,os
from untils.untils import  log

class Server(object):
    def __init__(self,host, username, password, port=22):
        self.hostname = host
        self.username = username
        self.password = password
        self.port = 22

    def _Transport(self):
        try:
            self.transport = paramiko.Transport((self.hostname, self.port))
            self.transport.connect(username=self.username, password=self.password)
        except Exception as e:
            log("Transport connect error!")
            log(e)

    def _Transport(self):
        try:
            self.transport = paramiko.Transport((self.hostname, self.port))
            self.transport.connect(username=self.username, password=self.password)
        except Exception as e:
            log("Transport connect error!")
            log(e)

    def _ssh(self):
        try:
            ssh = paramiko.SSHClient()
            ssh.load_system_host_keys()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            #ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(hostname=self.hostname, port=self.port, username=self.username, password=self.password)
            self.ssh = ssh
            log('ssh to {}:{}'.format(self.hostname,self.port))
        except Exception as e:
            log('ssh fail !')
            log(e)

    def download(self,remote, local):
        self._Transport()
        try:
                sftp = paramiko.SFTPClient.from_transport(self.transport)
                if sftp.listdir(remote):
                    files = sftp.listdir(remote) #这里需要注意，列出远程文件必须使用sftp，而不能用os
                    for f in files:
                            sftp.get(os.path.join(remote,f),os.path.join(local,f))
                else:
                    sftp.get(remote, local)

        except Exception as e:
                log("download error!")
                log(e)
        finally:
            self.transport.close()

    def upload(self,local, remote):
        self._Transport()
        try:

            sftp = paramiko.SFTPClient.from_transport(self.transport)
            if os.path.isdir(local):
                files = os.listdir(local)
                for f in files:
                    sftp.put(os.path.join(local, f), os.path.join(remote, f))
            else:
                sftp.put(local, os.path.join(remote))
        except Exception as e:
                log("upload error!")
                log(e)
        finally:
            self.transport.close()

    def excute_command(self, command):
        self._ssh()
        result = ''
        try:
            log('excute command:')
            log(command)
            stdin, stdout, stderr = self.ssh.exec_command(command)
            stdout = stdout.read()
            stderr = stderr.read()
            log('remote command response stdout:')
            log(stdout)
            log('remote command response stderr:')
            log(stderr)
            result = stdout + stderr
        except Exception as e:
            log('excute command error!')
            log(e)
        finally:
            self.ssh.close()
        return result
